package com.gnote.controller;

import com.alibaba.cloud.ai.dashscope.chat.DashScopeChatModel;
import com.gnote.pojo.ReReadingAdvisor;
import com.gnote.pojo.PlatformOptions;
import org.springframework.ai.chat.client.ChatClient;
import org.springframework.ai.chat.model.ChatModel;
import org.springframework.ai.chat.prompt.ChatOptions;
import org.springframework.ai.deepseek.DeepSeekChatModel;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.core.io.Resource;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import reactor.core.publisher.Flux;

import java.util.HashMap;
import java.util.Map;

@RestController
public class MultiPlatformController {
    private Map<String, ChatModel> platforms = new HashMap<>();

    public MultiPlatformController(
            DashScopeChatModel dashScopeChatModel,
            DeepSeekChatModel deepSeekChatModel) {
        platforms.put("dashscope", dashScopeChatModel);
        platforms.put("deepseek", deepSeekChatModel);
    }

    @RequestMapping(path = "/chat1", produces = "text/stream;charset=UTF-8")
    public Flux<String> chat(String message,
                             PlatformOptions platformOptions) {
        String platform = platformOptions.getPlatform();
        ChatModel chatModel = platforms.get(platform);

        ChatClient.Builder builder = ChatClient.builder(chatModel);
        ChatClient chatClient = builder
                //系统提示词
                .defaultSystem("""
                        #角色说明
                        你是一名专业的法律顾问
                        
                        ##回复格式
                        1.问题分析
                        2.相关依据
                        3.梳理和建议
                        
                        **特别注意:**
                        - 不承担法律责任
                        - 不生成涉敏、虚假内容
                        
                        """)
                //模型参数
                .defaultOptions(
                        ChatOptions.builder()
                                .temperature(platformOptions.getTemperature())
                                .model(platformOptions.getModel())

                                .build()
                ).build();

        return chatClient.prompt()
                //系统提示词
                .system(p -> p
                        .text("""
                                #角色说明
                                你是一名专业的法律顾问
                                
                                ##回复格式
                                1.问题分析
                                2.相关依据
                                3.梳理和建议
                                
                                **特别注意:**
                                - 不承担法律责任
                                - 不生成涉敏、虚假内容
                                
                                """)
                        .param("name", "gnote")
                        .param("sax", "男")
                        .param("age", 18))
                //用户输入
                .user(message).stream().content();
    }


    @RequestMapping(path = "/chat2", produces = "text/stream;charset=UTF-8")
    public Flux<String> chat2(String message,
                              PlatformOptions platformOptions,
                              @Value("classpath:/prompt.ts") Resource resource) {
        String platform = platformOptions.getPlatform();
        ChatModel chatModel = platforms.get(platform);

        ChatClient.Builder builder = ChatClient.builder(chatModel);
        ChatClient chatClient = builder
                .defaultSystem(resource)
                //模型参数
                .defaultOptions(
                        ChatOptions.builder()
                                .temperature(platformOptions.getTemperature())
                                .model(platformOptions.getModel())
                                .build()
                ).build();

        return chatClient.prompt()
                //系统提示词
                .system(p -> p
                        .param("name", "gnote")
                        .param("sax", "男")
                        .param("age", 18))
                //用户输入
                .user(message).stream().content();
    }


    @RequestMapping(path = "/reReading", produces = "text/stream;charset=UTF-8")
    public Flux<String> reReading(String message,
                                  PlatformOptions platformOptions) {
        String platform = platformOptions.getPlatform();
        ChatModel chatModel = platforms.get(platform);

        ChatClient.Builder builder = ChatClient.builder(chatModel);
        ChatClient chatClient = builder
                //模型参数
                .defaultOptions(
                        ChatOptions.builder()
                                .temperature(platformOptions.getTemperature())
                                .model(platformOptions.getModel())
                                .build()
                )
                .defaultAdvisors(new ReReadingAdvisor())
                .build();

        return chatClient.prompt()
                //用户输入
                .user(message).stream().content();
    }

}
